package com.hanbang.taitaigou.ui.activity.aboutme.coupon;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.model.GetMyCoupon;
import com.hanbang.taitaigou.ui.activity.base.BaseActivity;
import com.hanbang.taitaigou.utils.http.BitmapUtil;
import com.hanbang.taitaigou.utils.http.HttpCallBack;
import com.hanbang.taitaigou.utils.http.HttpLocalUtils;
import com.hanbang.taitaigou.utils.http.HttpRequestParams;
import com.hanbang.taitaigou.utils.http.JsonHelper;
import com.hanbang.taitaigou.utils.ui.ToastUtils;
import com.hanbang.taitaigou.utils.ui.UiUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * 优惠券
 * Created by fanxin on 2016/3/21.
 */
public class AboutCouponActivity extends BaseActivity {

    /**
     * 优惠券的list
     **/
    private ListView lv_coupon_list;
    private TextView textView;
    private List<GetMyCoupon> list;
    private ListViewAdapter listViewAdapter;

    private int i;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_coupon);
        initView();
        setOnClickListener();
        getMyCoupon();
    }

    /*
     * 设置点击事件
     */
    private void setOnClickListener() {
        Intent intent = getIntent();
        i = intent.getIntExtra("type", i);
        if (i == 1) {
            lv_coupon_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if(list.get(position).getIs_user() == 1){
                        ToastUtils.show(AboutCouponActivity.this,"该优惠券已使用过", Toast.LENGTH_LONG);
                    }else {
                        Intent intent = new Intent();
                        intent.putExtra("id", list.get(position).getId());
                        intent.putExtra("qian", list.get(position).getMoney());
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                }
            });
        }
    }


    @SuppressWarnings("unused")
    private View.OnClickListener onClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {

                default:
                    break;
            }
        }
    };

    /*
     * 初始化view
     */
    private void initView() {
        // 设置顶部的标题
        setTop(null, 0, getString(R.string.top_coupon), null, 0);
        setBackArrows(findViewById(R.id.top_bar_left_iv));
        lv_coupon_list = (ListView) findViewById(R.id.lv_coupon_list);
        textView = (TextView) findViewById(R.id.no);
        list = new ArrayList<GetMyCoupon>();
        listViewAdapter = new ListViewAdapter();
        lv_coupon_list.setAdapter(listViewAdapter);

    }

    /*
   * 获取用户的优惠券列表
   */
    private void getMyCoupon() {
        HttpRequestParams httpRequestParams = new HttpRequestParams("GetMyCoupon");
        httpRequestParams.addUserId(userData);
        httpManager.post(httpRequestParams, new HttpCallBack<String>(this, "正在加载...") {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    JSONArray js = jsonObject.getJSONArray("result");
                    list.addAll(JsonHelper.parseCollection(js, ArrayList.class, GetMyCoupon.class));

                    listViewAdapter.notifyDataSetChanged();
                    if (list.size() == 0) {
                        textView.setVisibility(View.VISIBLE);
                        lv_coupon_list.setVisibility(View.GONE);
                        return;
                    } else {
                        textView.setVisibility(View.GONE);
                        lv_coupon_list.setVisibility(View.VISIBLE);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    class ListViewAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // 常见的优化ViewHolder
            final ViewHolder viewHolder;
            if (null == convertView) {
                viewHolder = new ViewHolder();
//                convertView = getLayoutInflater().inflate(R.layout.item_about_coupon, null);
                convertView = UiUtils.getInflaterView(AboutCouponActivity.this, R.layout.item_about_coupon);
                viewHolder.iv_coupon_photo = (ImageView) convertView.findViewById(R.id.iv_coupon_photo);
                viewHolder.tv_coupon_time = (TextView) convertView.findViewById(R.id.tv_coupon_time);
                viewHolder.tv_coupon_money = (TextView) convertView.findViewById(R.id.tv_coupon_money);
                viewHolder.rl_beijing = (RelativeLayout) convertView.findViewById(R.id.rl_beijing);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //获取数据
            if (list.get(position).getIs_user() == 1) {
                viewHolder.rl_beijing.setBackgroundResource(R.drawable.aboutme_coupon_background2);
            }
            imageManager.bind(viewHolder.iv_coupon_photo, HttpLocalUtils.getHttpFileUrl(list.get(position).getImg_url()), BitmapUtil.getImageOptionsBuilder().build());
            String time = list.get(position).getTime().substring(0, 10);
            String addtime = list.get(position).getAdd_time().substring(0, 10);
            viewHolder.tv_coupon_time.setText("时间：" + time + " - " + addtime);
            viewHolder.tv_coupon_money.setText(list.get(position).getMoney() + "元");

            return convertView;
        }

        class ViewHolder {

            private ImageView iv_coupon_photo;
            private TextView tv_coupon_time;
            private TextView tv_coupon_money;
            private RelativeLayout rl_beijing;
        }
    }


}
